package org.example.controller;

import javax.validation.constraints.Pattern;

import org.example.pojo.Page;
import org.example.pojo.Result;
import org.example.pojo.User;
import org.example.service.UserService;
import org.example.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // private UserService userService = new UserServiceImpl();

    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        // UserService userService = new UserServiceImpl();

        //查询用户
        User u = userService.findByUserName(user.getUsername());
        if (u == null) {
            //没有占用
            //注册
            userService.register(user.getUsername(), user.getPassword());
            return Result.success();
        } else {
            //占用
            return Result.error("用户名已被占用");
        }
    }

    @GetMapping("/getList")
    public Result getList(@RequestParam int currentPage, @RequestParam int pageSize, @RequestParam(required = false) String nickname) {
        Page<User> page = userService.getByPages(currentPage, pageSize, nickname);
        return Result.success(page);
    }
}
